package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.domain.dto.SelectNurseGroupDto;
import com.blueocean_health.care.management.domain.groupvo.SelectNurseGroupVo;
import com.blueocean_health.care.management.service.SelectNurseGroupService;

/**
* 小组模块查询相关接口
* @author huyanqiu
* @date 2018年1月3日 下午6:39:48
* @version 版本号：1.0
*/
@RestController
@RequestMapping(value = {"/web/nurse/group"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class SelectNurseGroupController {
	
	@Autowired
	private SelectNurseGroupService selectNurseGroupService;
	
	/**
	 * 小组下拉列表  2018年5月16日17:57:49
	 * @return
	 */
	@RequestMapping("/pull")
	public SysResult pull() {
		List<PullDownDto> groupPull = selectNurseGroupService.getGroupPull();
		return SysResult.ok("success", groupPull);
	}
	
	/**
	 * 查询小组
	 * @return
	 */
	@RequestMapping("/query")
	public SysResult query(@RequestBody SelectNurseGroupDto selectNurseGroupDto) {
		if (StringUtils.isEmpty(selectNurseGroupDto.getGroupId())) {
			return SysResult.error("参数有误，请重新输入参数。");
		}
		return selectNurseGroupService.query(selectNurseGroupDto);
	}
	
	/**
	 * 小组列表
	 * @return
	 */
	@RequestMapping("/list")
	public SysResult list(@RequestBody SelectNurseGroupDto selectNurseGroupDto) {
		return selectNurseGroupService.list(selectNurseGroupDto);
	}
	
	/**
	 * 导出Excel
	 * @return
	 */
	@RequestMapping("/list-excel")
	public void exportListExcel(SelectNurseGroupDto selectNurseGroupDto, HttpServletResponse response) {
		try {
			List<SelectNurseGroupVo> list = selectNurseGroupService.listExcel(selectNurseGroupDto);
			
			String finalFileName = URLEncoder.encode("小组管理"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			ExcelUtil.getInstance().exportObj2Excel(toClient, list, SelectNurseGroupVo.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 选择成员
	 * @return
	 */
	@RequestMapping("/nurse/select-nurse")
	public SysResult selectNurse(@RequestBody(required=false) SelectNurseGroupDto selectNurseGroupDto) {
		return selectNurseGroupService.selectNurse(selectNurseGroupDto);
	}
	
	/**
	 * 模糊查询小组
	 * @return
	 */
	@RequestMapping("/select-group")
	public SysResult selectGroup(@RequestBody(required=false) SelectNurseGroupDto selectNurseGroupDto) {
		return selectNurseGroupService.selectGroup(selectNurseGroupDto);
	}
	
}
